package org.wendu.wdoa.apprev.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.wendu.wdoa.apprev.dto.leaveAddDto;
import org.wendu.wdoa.apprev.dto.leavequeryDto;
import org.wendu.wdoa.apprev.model.leave;
import org.wendu.wdoa.apprev.service.ApprevService;
import org.wendu.wdoa.common.CurrentUser;
import org.wendu.wdoa.common.Result;
import org.wendu.wdoa.common.WdoaConstants;
import org.wendu.wdoa.common.token.TokenUtils;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/apprev")
public class ApprevApi {
    @Autowired
    private ApprevService apprevService;

    @GetMapping("/leaveCurrentuserList")
    public Result CurrentUserLeave(@RequestHeader(WdoaConstants.HEADER_NAME_TOKEN) String clientToken, leavequeryDto leavequeryDto){
        CurrentUser currentUser = TokenUtils.getCurrentUser(clientToken);
        Map<String,Object> page = apprevService.getUserLeaveList(currentUser.getUserId(),leavequeryDto);
        return Result.OK(page);
    }
    @PostMapping("/addLeaveList")
    public Result AddLeave(@RequestHeader(WdoaConstants.HEADER_NAME_TOKEN) String clientToken,
                           @Validated @RequestBody leaveAddDto leaveAdd){
        CurrentUser currentUser = TokenUtils.getCurrentUser(clientToken);
        apprevService.insterLeave(currentUser.getUserId(),leaveAdd);
        return Result.OK();
    }
    @PostMapping("/updateLeaveList")
    public Result updateLeaveList(@RequestBody leave leave){
        apprevService.updateLeave(leave);
        return Result.OK();
    }
    @PostMapping("/endLeave")
    public Result endLeaveList(@RequestBody leave leave){
        apprevService.endLeave(leave);
        return Result.OK();
    }

}
